//
//  ItemZeroViewController.m
//  Lovelife
//
//  Created by apple on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ItemZeroViewController.h"
#import "AFNetworking.h"
#import "ItemZeroModel.h"
#import "ItemZeroCell.h"
#import "UIImageView+WebCache.h"
#import "ItemZeroRightBarButtonItemCell.h"
#import "WebViewController.h"
#import "MJRefresh.h"
#import "NightManager.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ItemZeroViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *modelArr;

@property (nonatomic,retain)UITableView *TableView;

@property (nonatomic,retain)UIActivityIndicatorView *acIntor;

@property (nonatomic,retain)UIView *menuView;

@property (nonatomic,assign)NSInteger number;
@property (nonatomic,assign)BOOL  temp;



@end

@implementation ItemZeroViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
        
        self.number = 1;
        self.temp = YES;
    }
    return self;
}

-(void)dealloc
{
    
    [_modelArr release];
    [_TableView release];
    [_menuView release];
    [super dealloc];
    
}


-(void)getData
{
    if (self.temp == YES) {
        [self.acIntor startAnimating];

    }
    
    AFHTTPRequestOperationManager *manager  = [ AFHTTPRequestOperationManager manager];
    
    NSString * str = @"%E5%AE%B6%E5%B8%B8%E8%8F%9C";
    
    [manager GET:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?category=%@&p=%ld&pagesize=12",str,self.number]parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSMutableArray *tempArr = [responseObject objectForKey:@"results"];
        for (NSMutableDictionary *dic in tempArr) {
            ItemZeroModel *zero = [[ItemZeroModel alloc]init];
            
            [zero setValuesForKeysWithDictionary:dic];
            
            [self.modelArr addObject:zero];
        }
        NSLog(@"%ld",self.modelArr.count);
        if (self.temp == YES) {
            [self.acIntor stopAnimating];
            
            [self createView];
           
            
        }
        [self.TableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误信息%@",error);
    }];
    
    
    
}

-(void)createView
{
    

    
    self.TableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.TableView];
    
    [self.TableView release];
    
    self.TableView.dataSource = self;
    
    self.TableView.delegate = self;
    
    [self.TableView addHeaderWithCallback:^{
        [self header];
    }];
    
    [self.TableView addFooterWithCallback:^{
        [self footer];
    }];
    
    
    
}


-(void)header
{
    [self.modelArr removeAllObjects];
    [self.TableView reloadData];
    [self getData];
    self.temp = NO;
    [self.TableView headerEndRefreshing];

}
-(void)footer
{
    self.number ++;
    self.temp = NO;
    
    [self getData];
    [self.TableView footerEndRefreshing];


}

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark 创建网路延迟转轮
    self.acIntor = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.acIntor setCenter:self.view.center];
    [self.acIntor setBackgroundColor:[UIColor grayColor]];
    [self.acIntor setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.acIntor];
    [_acIntor release];
    [self getData];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];

    
    //创建rightBarButtonItem
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_menu_bg"] style:UIBarButtonItemStylePlain target:self action:@selector(menuClickAction:)];

    
}
//-(void)createMenuTableView
//{
//
//    self.menuView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//    
//    self.menuView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4];
//    
//    self.menuView.alpha = 0;
//
//    [self.view addSubview:self.menuView];
//
//}

- (void)menuClickAction:(id)sander
{
    
//    [UIView animateWithDuration:0.5
//                     animations:^{
//                         
//                         self.menuView.alpha = 1;
// 
//                     }
//                     completion:^(BOOL finished) {
//
//                     }];

    
    
    
    

}


//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//    
//    [UIView animateWithDuration:0.5 animations:^{
//        
//        self.menuView.alpha = 0;
//        
//    } completion:^(BOOL finished) {
//   
//    }];
//}

//segmentcontroller 点击方法


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return  self.modelArr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuse = @"reuse";
    
    ItemZeroCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    if (!cell) {
        cell =  [[[ItemZeroCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    
    ItemZeroModel *zero = self.modelArr[indexPath.row];
    
    cell.titleLB.text = zero.title;
    
    
    NSURL *url = [NSURL URLWithString:zero.thumb];
    
    [cell.thumbImg sd_setImageWithURL:url];
    
        return cell;

    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return 244;
    
    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.menuView.alpha == 0) {
   

    
#pragma mark 传递ID给DetailsView
        WebViewController *web = [[WebViewController alloc]init];
    
    ItemZeroModel *zero = self.modelArr[indexPath.row];
    
        web.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",zero.ID];
        zero.url = web.url;
        
        web.model = zero;

        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
   
//    }
  
    
}

-(void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
    

}
-(void)setLightDayModel
{

    [NightManager setBackgroundColorWithView:self.view];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
